package myJs

import com.karasiq.bootstrap.Bootstrap.default._
import myJs.Utils._
import myJs.myPkg._
import org.querki.jquery._
import org.singlespaced.d3js.Ops._
import org.singlespaced.d3js.d3
import shared.Implicits._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}


/**
  * Created by yz on 2019/4/15
  */
@JSExportTopLevel("Predict")
object Predict {

  @JSExport("init")
  def init = {
    bootStrapValidator
    //    showResult("322")

  }


  @JSExport("myRun")
  def myRun = {
    val bv = jQuery("#form").data("bootstrapValidator")
    bv.validate()
    val valid = bv.isValid().asInstanceOf[Boolean]
    if (valid) {
      val data = $("#form").serialize()
      val url = g.jsRoutes.controllers.PredictController.predict().url.toString
      val index = layer.alert(element, layerOptions)
      val ajaxSettings = JQueryAjaxSettings.url(url).
        `type`("post").data(data).success { (data, status, e) =>
        layer.close(index)
        val rs = data.asInstanceOf[js.Dictionary[js.Any]]
        if (rs.myGet("valid") == "false") {
          Swal.swal(SwalOptions.title("错误").text(rs.myGet("message")).`type`("error"))
          $("#result").hide()
        } else {
          val missionId = rs.myGet("missionId")
          PredictResult.showResult(missionId)
          g.reduceTime()

        }
      }
      $.ajax(ajaxSettings)
    }

  }


  def bootStrapValidator = {
    val dict = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "glyphicon glyphicon-remove",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "sampleId" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "样本号不能为空!"
            ),
          )
        ),
        "age" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "年龄不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 150,
              "message" -> "年龄必须处于0到150之间！!"
            ),
          )
        ),
        "ast" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "天门冬氨酸氨基转移酶不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 1000,
              "message" -> "天门冬氨酸氨基转移酶必须处于0到1000之间！!"
            ),
          )
        ),
        "urea" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "尿素不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "尿素必须处于0到100之间！!"
            ),
          )
        ),
        "ldlch" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "低密度脂蛋白-胆固醇不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "低密度脂蛋白-胆固醇必须处于0到100之间！!"
            ),
          )
        ),
        "apoa" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "载脂蛋白A1不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "载脂蛋白A1必须处于0到100之间！!"
            ),
          )
        ),
        "fbg" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "空腹血糖不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 100,
              "message" -> "空腹血糖必须处于0到100之间！!"
            ),
          )
        ),
        "tbil" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "总胆红素不能为空!"
            ),
            "between" -> js.Dictionary(
              "min" -> 0,
              "max" -> 500,
              "message" -> "总胆红素必须处于0到500之间！!"
            ),
          )
        ),
      )
    )
    g.$("#form").bootstrapValidator(dict)

  }


}
